<template>
  <div style="overflow-y: auto;">
    <div class="header-container">
      <h1>我的课堂</h1>
    </div>
    <div class="controls-container">
      <!-- 操作按钮 -->
      <el-row :gutter="10" style="margin-bottom: 15px;">
        <el-col :span="1.5">
          <el-button :disabled="studentCourseIdList.length === 0" icon="Delete" plain type="danger"
                     @click="handleBatchDelete">批量退课
          </el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: 68px">
          <refresh-and-hide @refresh="handleRefresh" @toggle-search="handleToggleSearch"/>
        </el-col>
      </el-row>
      <!-- 搜索栏 -->
      <el-form v-show="showSearch" :inline="true" :model="queryParams">
        <el-form-item label="名称:">
          <el-input v-model="queryParams.name" clearable placeholder="请输入课程名"
                    style="width: 150px" @input="handleQuery"/>
        </el-form-item>
        <el-form-item label="任课教师:">
          <el-input v-model="queryParams.teacherName" clearable placeholder="请输入任课教师"
                    style="width: 150px" @input="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <svg-icon icon-file-name="white-search" style="color: #1abc9c"></svg-icon>
            搜索
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="scroll-container">
      <!-- 表格展示 -->
      <el-table v-loading="loading" :data="studentCourseList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <!-- 表格选择列 -->
        <el-table-column align="center" type="selection" width="55"></el-table-column>
        <!--缩略图-->
        <el-table-column align="center" label="课程封面" prop="courseImage" width="180">
          <template #default="scope">
            <el-image :lazy="true" :src="scope.row.courseImage" class="image-cover" fit="cover">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <!-- 课程名 -->
        <el-table-column align="center" label="课程名" prop="name" width="160"></el-table-column>
        <!-- 任课教师 -->
        <el-table-column align="center" label="任课教师" prop="teacherName" width="90"></el-table-column>
        <!-- 课程描述 -->
        <el-table-column align="center" label="课程描述" prop="description" width="160"></el-table-column>
        <!-- 课程学分 -->
        <el-table-column align="center" label="课程学分" prop="credits" sortable="custom" width="120">
        </el-table-column>
        <!-- 结课时间 -->
        <el-table-column align="center" label="结课时间" prop="endTime" sortable width="130">
          <template #default="scope">
            <div class="time">
              <svg-icon icon-file-name="time"></svg-icon>
              <span style="margin-left: 10px">{{ scope.row.endTime }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="上课时间" prop="upClassTime" sortable width="130">
          <template #default="scope">
            <div class="time">
              <span style="margin-left: 10px">{{formatClassTime(scope.row.week, scope.row.onClass)}}</span>
            </div>
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column align="center" label="选课时间" prop="selectTime" sortable width="130">
          <template #default="scope">
            <div class="time">
              <svg-icon icon-file-name="time"></svg-icon>
              <span style="margin-left: 10px">{{ scope.row.selectTime }}</span>
            </div>
          </template>
        </el-table-column>
        <!-- 更新时间 -->
        <el-table-column align="center" label="课程信息更新时间" prop="updateTime" sortable width="130">
          <template #default="scope">
            <div class="time">
              <svg-icon icon-file-name="time"></svg-icon>
              <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
            </div>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column align="center" label="操作" width="150">
          <template #default="scope">
            <el-button class="sdf" link type="primary" @click="handleWidth(scope.row.selectInfoId)">
              <svg-icon icon-file-name="edit"></svg-icon>
              退课
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <pagination v-if="total > 0" :limit="pagInfo.pageSize" :page="pagInfo.currentPage" :total="total"
                style="float: right;" @pagination="getStudentCoursesList"
                @update_limit="pagInfo.pageSize = $event"
                @update_page="pagInfo.currentPage = $event"/>

  </div>
</template>

<script>
import refreshAndHide from "@/common/refreshAndHide.vue";
import Pagination from "@/common/pagination.vue";
import {getStudentCourseList, dropCourse, dropCourseOnList} from "@/apis/student";

export default {
  // 组件注册，允许在模板中使用导入的组件
  components: {
    Pagination, // 分页组件
    refreshAndHide // 刷新和隐藏组件
  },
  // 组件的响应数据对象
  data() {
    return {
      // 控制搜索表单显示的布尔值
      showSearch: true,
      // 搜索参数对象
      queryParams: {
        name: '', // 课程名
        teacherName: '', // 任课老师姓名
      },
      //分页信息
      pagInfo: {
        currentPage: 1,
        pageSize: 10
      },
      // 存储选中的课程ID列表
      studentCourseIdList: [],
      // 存储课程列表的数组
      studentCourseList: [],
      // 控制表格加载状态的布尔值
      loading: false,
      // 存储总课程数量的变量
      total: 0,
    };
  },
  methods: {
    formatClassTime(week, onClass) {
      const weekDays = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六','星期日'];
      const classTimes = ['第一节课', '第二节课', '第三节课', '第四节课'];
      return `${weekDays[week - 1]}${classTimes[onClass - 1]}`;
    },
    // 处理学生退课
    handleWidth(selectInfoId) {
      let data = {
        selectInfoId: selectInfoId
      }
      dropCourse(data).then(res=>{
        if (res.data.code === 1){
          if (res.data.code === 1) {
            this.$message.success(res.data.data);
            this.getStudentCoursesList();
          } else {
            this.$message.error(res.data.msg);
          }
        }
      })
    },
    // 前端排序函数
    handleSortChange({prop, order}) {
      if (!order) {
        // 如果没有排序顺序，可能是恢复到未排序状态
        this.getStudentCoursesList();
        return;
      }
      this.studentCourseList.sort((a, b) => {
        if (order === 'ascending') {
          return a[prop] - b[prop]; // 升序排序
        } else {
          return b[prop] - a[prop]; // 降序排序
        }
      });
    },
    // 处理搜索显示/隐藏的逻辑
    handleToggleSearch() {
      this.showSearch = !this.showSearch;
    },
    // 处理刷新数据的逻辑
    handleRefresh() {
      this.getStudentCoursesList();
      this.$message.success('列表已刷新');
    },
    // 搜索课程的方法
    handleQuery() {
      this.loading = true;
      // 重置当前页为第一页
      this.pagInfo.currentPage = 1;
      const pagination = {
        currentPage: this.pagInfo.currentPage,
        pageSize: this.pagInfo.pageSize
      }
      const queryParams = {
        name: this.queryParams.name,
        teacherName: this.queryParams.teacherName,
      }
      const DTOdata = {
        pagination,
        queryParams//只有搜索时才会带上
      }
      getStudentCourseList(DTOdata).then(res => {
        if (res.data.code === 1) {
          res.data.data.list.forEach((studentCourse) => {
            studentCourse.selectTime = this.$common.dateFormat(studentCourse.selectTime);
            studentCourse.endTime = this.$common.dateFormat(studentCourse.endTime);
            studentCourse.updateTime = this.$common.dateFormat(studentCourse.updateTime);
          });
          this.studentCourseList = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    // 处理表格行选择事件
    handleSelectionChange(selection) {
      this.studentCourseIdList = selection.map(item => item.selectInfoId);
    },
    //批量删除方法
    handleBatchDelete() {
      if (this.studentCourseIdList.length === 0) {
        this.$message({
          type: 'info',
          message: '请选择要退课的课程'
        });
        return;
      }
      // 确认弹窗
      this.$confirm('确定要退选选中的课程吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里发送删除请求
        let data = {
          ids: this.studentCourseIdList
        }
        dropCourseOnList(data).then(res=>{
          if (res.data.code === 1){
            this.$message.success(res.data.data);
            this.getStudentCoursesList();
          } else {
            this.$message.error(res.data.msg);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 获取课程列表的方法
    async getStudentCoursesList() {
      this.loading = true;
      const pagination = {
        currentPage: this.pagInfo.currentPage,
        pageSize: this.pagInfo.pageSize
      }
      const queryParams = null;
      const DTOdata = {
        pagination,
        queryParams
      }
      await getStudentCourseList(DTOdata).then(res => {
        if (res.data.code === 1) {
          res.data.data.list.forEach((studentCourse) => {
            studentCourse.selectTime = this.$common.dateFormat(studentCourse.selectTime);
            studentCourse.updateTime = this.$common.dateFormat(studentCourse.updateTime);
            studentCourse.endTime = this.$common.dateFormat(studentCourse.endTime);
          });
          this.studentCourseList = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    }
  },
  created() {
    this.getStudentCoursesList();
  }
};
</script>

<style scoped>
.header-container {
  display: flex;
  justify-content: space-between; /* 使子元素之间的间距最大化，从而将标题推向左边，将搜索栏推向右边 */
  align-items: center; /* 垂直居中对齐子元素 */
  padding: 0 15px; /* 根据需要调整，为容器添加一些内边距 */
  margin-bottom: 20px; /* 根据需要调整，为标题和搜索栏下方留出一些空间 */
}

.controls-container {
  display: flex;
  justify-content: space-between; /* 使子元素分布在容器的两端 */
  align-items: center; /* 垂直居中对齐子元素 */
}


h1 {
  margin: 0; /* 移除默认的外边距 */
  font-size: 30px; /* 根据需要调整标题的字体大小 */
}


.scroll-container {
  max-height: 646px; /* 您可以根据需要调整这个值 */
  overflow-y: auto;
}

.sdf {
  background: transparent;
  border-color: transparent;
  color: #76d7ff;
}

.image-cover {
  width: 100px;
  height: 60px;
  border-radius: 5px;
  overflow: hidden;
  cursor: pointer;

  .image-slot {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f7fa;
    color: #909399;
  }
}

.time {
  display: flex;
  align-items: center;
}
</style>

